//
//  SinaVisitorView.swift
//  WeiBo
//
//  Created by Apple on 15/12/30.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

// 代理先声明 -- 必须继承自 NSObjectProtocol,这样可以使用我们的 weak

protocol SinaVisitorViewDelegate: NSObjectProtocol {
    
    // 登录回调方法
    func didLoginMethod()
    // 注册回调方法
    func didRegisterMethod()
    
}

class SinaVisitorView: UIView {

    weak var delegate: SinaVisitorViewDelegate?
    
    
    // 构造函数里,调用
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        // 
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
   
    
    
    //MARK:--动画方法
    func startAnimation(){
    
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        
        // 属性
        animation.toValue = 2 * M_PI
        
        animation.repeatCount = MAXFLOAT
        
        animation.duration = 20
        // 当完成动画之后,或者 试图跑到后台之后, 会默认移除,我们把 removedOnCompletion 改成false就不会移除
        animation.removedOnCompletion = false
        
        animationImageView.layer.addAnimation(animation, forKey: nil)
        
    }
    
    // 定义一个方法,供子类去调用
    // 我们通过 设置图片为可选项来达到,判断是否需要显示首页小房子动画
    func setupInfo(message: String,image: String?){
    
        if let Oimge = image {
            
            // 这里是非首页,然后让 动画隐藏
            animationImageView.hidden = true
            
            houseImgeView.image = UIImage(named: Oimge)
        } else {
            animationImageView.hidden = false
        }
        
        
        messageLabel.text = message
        
    }
    
    //MARK:--登录和注册按钮点击事件
    //registerMethod
    func registerMethod(){
    
        printLog("注册")
        // OC里是不是判断了 
        // 可选项相当于做了判断
        delegate?.didRegisterMethod()
        
        // 如果 你用 delegate!.didRegisterMethod()
        // 必须得有一个代码
    }
    
    func loginMethod(){
        printLog("登录")
        delegate?.didLoginMethod()
    }
    
    //MARK:--懒加载 -- 是我们的第一步
    private lazy var animationImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        
        img.sizeToFit()
        
        return img
    }()
    
    
 /// 遮盖试图
    private lazy var maskImageView: UIImageView = {
    
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        return img
    }()
    
///  首页小房子
   private lazy var houseImgeView: UIImageView = {
    
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_image_house")
        
        return img
    }()
    
///  提示语
   private lazy var messageLabel: UILabel  = {
    
        let lb = UILabel()
        
        // 设置约束
        lb.text = "关注一些人，回这里看看有什么惊喜  关注一些人，回这里看看有什么惊喜"
        
        lb.textColor = UIColor.darkGrayColor()
        
        lb.textAlignment = .Center
        
        //设置numberOfLines
        lb.numberOfLines = 0
        
        return lb
    }()
    
    
///  注册按钮
   private lazy var registerButton: UIButton = {
    
        let button = UIButton(type: UIButtonType.Custom)
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        button.setTitle("注册", forState: UIControlState.Normal)
        
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
    
    // 添加方法
    button.addTarget(self , action: "registerMethod", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
///  登录按钮
   private lazy var loginButton: UIButton = {
        
        let button = UIButton(type: UIButtonType.Custom)
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        button.setTitle("登录", forState: UIControlState.Normal)
        
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
    
    // 按钮方法
    button.addTarget(self , action: "loginMethod", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()

}

// 进行代码分块 -- extension 是关键字,后边的类是这个类的名字
// extension 不能放属性
extension SinaVisitorView {

    
    
    //MARK:--添加到试图里
    private func setupUI(){
        
        // 改一个背景颜色
        backgroundColor = UIColor(white: 237/255.0, alpha: 1.0)
        
        // 第二步,添加试图
        addSubview(animationImageView)
        addSubview(maskImageView)
        addSubview(houseImgeView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 添加约束
        
        
        // 首页的动画图片约束 -- 水平居中,垂直居中
        // 关闭 系统  autosizing
        // 第三步 -- 添加约束
        animationImageView.translatesAutoresizingMaskIntoConstraints = false
        maskImageView.translatesAutoresizingMaskIntoConstraints = false
        houseImgeView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
        // 手动添加约束
        /*
        item: <#T##AnyObject#>,  -- 要给哪个控件设置约束
        attribute: <#T##NSLayoutAttribute#>,  -- 设置对齐的方向
        relatedBy: <#T##NSLayoutRelation#>,  有三个选项,我们一般选用 equal
        toItem: <#T##AnyObject?#>,  -- 参照于哪个试图
        attribute: <#T##NSLayoutAttribute#>,
        multiplier: <#T##CGFloat#>,  系数 0~1
        constant: <#T##CGFloat#>    微调值
        
        Create constraints explicitly.  Constraints are of the form "view1.attr1 = view2.attr2 * multiplier + constant
        */
        addConstraint(NSLayoutConstraint(item: animationImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: animationImageView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        // 遮罩试图的约束 -- 先让他和 父试图,宽高相同,然后水平居中,垂直居中
        addConstraint(NSLayoutConstraint(item: maskImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: maskImageView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: maskImageView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: 0))

        addConstraint(NSLayoutConstraint(item: maskImageView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Height, multiplier: 1.0, constant: -350))
        
        //首页的小房子
        addConstraint(NSLayoutConstraint(item: houseImgeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: houseImgeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        //提示label --
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: animationImageView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 10))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        
        // 注册按钮 -- 宽 100,高 30 , 左边和 messageLabel的左边对其,顶部和其底部对齐
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 30))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 10))
        
        
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 30))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 10))
        
    }
    
}
